package net.hn.hnms.biz.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.material.domain.PurchasePlan;
import net.hn.hnms.biz.material.mapper.PurchasePlanMapper;
import net.hn.hnms.biz.material.request.PurchasePlanReq;
import net.hn.hnms.biz.material.service.IPurchasePlanService;
import net.hn.hnms.biz.material.vo.PurchasePlanVO;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ly
 * 年度采购计划表服务层实现
 * @date 2022-08-26
 */

@Service
public class PurchasePlanServiceImpl implements IPurchasePlanService {

    private PurchasePlanMapper purchasePlanMapper;

    public PurchasePlanServiceImpl(PurchasePlanMapper purchasePlanMapper) {
        this.purchasePlanMapper = purchasePlanMapper;
    }

    @Override
    public ResponsePage<PurchasePlanVO> page(PurchasePlanReq req, PageQuery pageQuery) {
        Page<PurchasePlan> page = purchasePlanMapper.selectPage(pageQuery.build(), buildQuery(req));
        List<PurchasePlanVO> list =page.getRecords().stream().map(o ->{
            PurchasePlanVO vo = new PurchasePlanVO();
            BeanUtil.copyProperties(o, vo);
            return vo;
        }).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<PurchasePlan> 查询条件
     */
    private LambdaQueryWrapper<PurchasePlan> buildQuery(PurchasePlanReq req){
        return new LambdaQueryWrapper<PurchasePlan>()
                .like(StrUtil.isNotBlank(req.getPlanCode()), PurchasePlan::getPlanCode, req.getPlanCode())
                .like(StrUtil.isNotBlank(req.getDepartment()), PurchasePlan::getDepartment, req.getDepartment())
                .eq(StrUtil.isNotBlank(req.getPlanType()), PurchasePlan::getPlanType, req.getPlanType())
                .eq(StrUtil.isNotBlank(req.getProjectType()), PurchasePlan::getProjectType, req.getProjectType())
                .eq(StrUtil.isNotBlank(req.getApprovalState()), PurchasePlan::getApprovalState, req.getApprovalState())
                .eq(StrUtil.isNotBlank(req.getAssignState()), PurchasePlan::getAssignState, req.getAssignState())
                .eq(StrUtil.isNotBlank(req.getMineCode()), PurchasePlan::getMineCode, req.getMineCode())
                .like(StrUtil.isNotBlank(req.getMaterialCode()), PurchasePlan::getMaterialCode, req.getMaterialCode())
                .like(StrUtil.isNotBlank(req.getSpecificationModel()), PurchasePlan::getSpecificationModel, req.getSpecificationModel())
                .like(StrUtil.isNotBlank(req.getProjectName()), PurchasePlan::getProjectName, req.getProjectName())
                .in(CollUtil.isNotEmpty(req.getPlanTypeArr()), PurchasePlan::getPlanType, req.getPlanTypeArr())
                ;
    }
}
